﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Model;
using BLL;
namespace stock.GuPiao
{
    public partial class AddGuPiao : System.Web.UI.Page
    {
        StockBLL stoBLL = new StockBLL();
        StockAlertBLL alertBL = new StockAlertBLL();
        StockAlert AlertModel=new StockAlert();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["id"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                hfId.Value = Session["id"].ToString();
                List<StockAlert> alertList = new List<StockAlert>();
                alertList = alertBL.GetModelList(" CustomerId=" + Session["id"].ToString());
                if (alertList != null)
                {
                    if (alertList.Count >8)
                    {
                        string script;
                        script = "<script language='Javascript'>alert('个人用户最多添加8条数据！');window.location.href='stock.aspx';</script>";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                        Wizard1.Enabled = false;
                    }
                }
            }
        }

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (Wizard1.ActiveStepIndex == 0)
            {
                String code = tbCode.Text.Trim();
                if (!HaveStock(code))
                {
                    string script;
                    script = "<script language='Javascript'>alert('股票代码错误！');</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                    Wizard1.ActiveStepIndex = -1;
                    Wizard1.ActiveStepIndex = 0;
                }
                CustomerBLL customBL = new CustomerBLL();
                int id = int.Parse(hfId.Value);
                if (!customBL.Authenticated(id))
                {
                    string script;
                    script = "<script language='Javascript'>alert('需要邮箱验证才能以便确认您邮箱是否填正确！');</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                    Wizard1.ActiveStepIndex = -1;
                    Wizard1.ActiveStepIndex = 0;
                }
            }
        }

        private Boolean HaveStock(string code)
        {
            Common.Model.Stock stockModel = stoBLL.GetModel(code);
            if (stockModel == null)
            {
                return false;
            }
            lbCode.Text = stockModel.Name + "(" + code+")";
            return true;
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            String code = tbCode.Text.Trim();
            decimal UpPrice = Math.Abs(decimal.Parse( tbUpPrice.Text.Trim()));
            decimal UpMargin =  Math.Abs(decimal.Parse(tbUpMargin.Text.Trim()));
            decimal UpAuto =  Math.Abs(decimal.Parse(tbUpAuto.Text.Trim()));
            decimal DownPrice = Math.Abs(decimal.Parse(tbDownPrice.Text.Trim()));
            decimal DownMargin = 0- Math.Abs(decimal.Parse(tbDownMargin.Text.Trim()));
            decimal DownAuto =  Math.Abs(decimal.Parse(tbDownAuto.Text.Trim()));
            String script;
            if (DownMargin > 100)
            {
                script = "<script language='Javascript'>alert('跌幅不可能达到100%！');</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                return;
            }
            if (DownPrice >= UpPrice)
            {
                script = "<script language='Javascript'>alert('下跌提醒价格不能超过上扬价格');</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                return;
            }
            AlertModel.Code = code;
            AlertModel.DownAlertMargin = DownMargin / 100;
            AlertModel.DownAlertPrice = DownPrice;
            AlertModel.DownAuto = DownAuto;
            AlertModel.UpAlertMargin = UpMargin / 100;
            AlertModel.UpAlertPrice = UpPrice;
            AlertModel.UpAuto = UpAuto;
            AlertModel.CreateTime = DateTime.Now;
            AlertModel.DownChangeTime = DateTime.Now;
            AlertModel.UPChangeTime = DateTime.Now;
            AlertModel.CustomerId =int.Parse( hfId.Value);
            int back = alertBL.Add(AlertModel);
            if (back > 0)
            {
                script = "<script language='Javascript'>alert('添加成功！');location.href='/gupiao/stock.aspx'</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                return;
            }
            script = "<script language='Javascript'>alert('添加失败！');location.href='/gupiao/stock.aspx'</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
        }
    }
}